let navbox = document.querySelector("#nav");
// 图片正则
var regExp = /^<[^<]+\s+src="([^''""]*)"/;

fetch("http://chst.vip:1234/api/getindexmenu").then(function (response) {
    return response.json();
}).then(function (res) {
    // console.log(res);

    var txt1 = "";
    res.result.forEach(item => {
        console.log(item);
        var img = regExp.exec(item.img)[1];
        // console.log(img);
        txt1 += `<div class="smallbox">
                <a href="./pages/${item.titlehref}">
                    <span>
                        <img src="http://chst.vip:8081/for-stu/wfzh/${img}">
                    </span>
                </a>
                <p class="pname">${item.name}</p>
            </div>
            `
            
    });
        navbox.innerHTML=txt1;
        console.log(navbox);

        var abox=document.querySelectorAll("a");


})


// 商品折扣
let probox=document.querySelector(".product ul");
fetch("http://chst.vip:1234/api/getmoneyctrl").then(function(response){
    return response.json();
}).then(function(res2){
    console.log(res2);
    
    
    res2.result.forEach(item => {
        // console.log(item);
        var oli=document.createElement("li");
        // 图片正则
        let regExp2 = /(?<=imgurl=)[^'"]*/;
        var img2 = regExp2.exec(item.productImgSm)[0];
        // console.log(img2);

       // 获取li的id值
        oli.id=item.productId;

        console.log(item.productId);
        console.log(oli.id);
        oli.innerHTML = `
                        <a href="#" class="clearfix">
                            <div class="img_fl">
                                <img src="${img2}" alt="">
                            </div>
                            <!-- 右边文本 -->
                            <div class="infoR">
                                <!-- 标题 -->
                                <div class="title">
                                    <h4>
                                        ${item.productName}
                                        <span>${item.productPinkage}</span>
                                    </h4>
                                </div>
                                <!-- 日期、评论数 -->
                                <div class="other">
                                    <div class="mall_fl">
                                        ${item.productFrom} |${item.productTime}
                                    </div>
                                    <div class="pinglun">
                                        <img src="./asset/images/评论.png" alt="">
                                        <span> ${item.productComCount}</span>
                                    </div>
                                </div>
                            </div>
                        </a> `
                        console.log(oli);
                    probox.appendChild(oli);
    });

    


    let oli=document.querySelectorAll(".info li");
    let proimg=document.querySelectorAll(".img_fl img");
    let protext=document.querySelectorAll(".infoR");
    console.log(proimg);
    console.log(protext);
    console.log(oli);

    oli.forEach(function(value){
        value.addEventListener('click',function(e){
            let target = e.target;
            if(target.nodeName=="IMG"){
                target=target.parentNode.parentNode.parentNode;

            }
            if(target.nodeName=="DIV"){
                target=target.parentNode.parentNode;

            }
            if(target.nodeName=="H4"){
                target=target.parentNode.parentNode.parentNode.parentNode;

                
            }

            //获取li标签的id编号
            let productId = target.id;
            console.log(productId);
            location.href="../pages/product_details.html?productid="+productId;
            console.log(location.href);
        })
    })

})





